package cn.study.zjan.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.OvershootInterpolator;
import android.widget.Button;

import com.ogaclejapan.arclayout.ArcLayout;

import java.util.ArrayList;
import java.util.List;

import cn.study.zjan.MBaseActivity;
import cn.study.zjan.R;
import cn.study.zjan.common.lib.viewioc.ContentViewById;
import cn.study.zjan.common.lib.viewioc.ViewById;
import cn.study.zjan.util.AnimatorUtils;

/**
 * Item菜单飞出动画
 */
@ContentViewById(R.layout.activity_path_anim)
public class PathAnimActivity extends MBaseActivity {

    @ViewById(R.id.arc_layout)
    ArcLayout mArcLayout;

    @ViewById(R.id.start_btn)
    Button mStartBtn;

    boolean mIsEnd = true;//动画是否结束

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_path_anim, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 开启动画
     *
     * @param view
     */
    public void startAnim(View view) {

        if (mIsEnd) {
            mIsEnd = false;

            boolean select = view.isSelected();
            if (select) {
                hideMenu();
            } else {
                showMenu();
            }

            view.setSelected(!select);
        }
    }


    /**
     * 显示菜单动画
     */
    private void showMenu() {
        mArcLayout.setVisibility(View.VISIBLE);

        List<Animator> animators = new ArrayList<>();
        for (int i = 0; i < mArcLayout.getChildCount(); i++) {
            animators.add(createShowAnimator(mArcLayout.getChildAt(i)));
        }

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(400);
        animatorSet.setInterpolator(new OvershootInterpolator());
        animatorSet.playTogether(animators);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mIsEnd = true;
            }
        });
        animatorSet.start();
    }

    /**
     * 隐藏菜单动画
     */
    private void hideMenu() {
        List<Animator> animators = new ArrayList<>();
        for (int i = 0; i < mArcLayout.getChildCount(); i++) {
            animators.add(createHideAnimator(mArcLayout.getChildAt(i)));
        }

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(400);
        animatorSet.setInterpolator(new AnticipateInterpolator());
        animatorSet.playTogether(animators);
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mIsEnd = true;
                mArcLayout.setVisibility(View.INVISIBLE);
            }
        });
        animatorSet.start();
    }


    /**
     * 创建显示菜单动画
     *
     * @param view
     * @return
     */
    private Animator createShowAnimator(View view) {

        float tx = mStartBtn.getX() - view.getX();
        float ty = mStartBtn.getY() - view.getY();

        Animator animator = ObjectAnimator.ofPropertyValuesHolder(
                view,
                AnimatorUtils.rotation(0, 720),
                AnimatorUtils.translationX(tx, 0),
                AnimatorUtils.translationY(ty, 0));

        return animator;
    }

    /**
     * 创建隐藏菜单动画
     *
     * @param view
     * @return
     */
    private Animator createHideAnimator(final View view) {
        float tx = mStartBtn.getX() - view.getX();
        float ty = mStartBtn.getY() - view.getY();

        Animator animator = ObjectAnimator.ofPropertyValuesHolder(
                view,
                AnimatorUtils.rotation(0, 720),
                AnimatorUtils.translationX(0, tx),
                AnimatorUtils.translationY(0, ty));
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.setTranslationX(0f);
                view.setTranslationY(0f);
            }
        });

        return animator;
    }
}
